#include "input.h"
#include <vector>

std::vector<CInputListener*> CInputManager::m_vInputListeners = std::vector<CInputListener*>();
vec2f CInputManager::m_v2f_MousePos;
vec2f CInputManager::m_v2f_MouseInitial;
vec2f CInputManager::m_v2f_LastMousePos;

CInputManager::CInputManager()
{
	m_v2f_MousePos.x = 0;
	m_v2f_MousePos.y = 0;
	m_v2f_MouseInitial.x = 0;
	m_v2f_MouseInitial.y = 0;
	m_bMouseAlreadyDown = false;
}

CInputManager::~CInputManager()
{
	
}

void CInputManager::Update()
{
	int32_t mouseX, mouseY;
	mouseX = mouseY = 0;
	CInputManager::GetMousePosition(mouseX, mouseY);
	m_v2f_MousePos.x = static_cast<float>(mouseX);
	m_v2f_MousePos.y = static_cast<float>(mouseY);

	bool down = IsMouseButtonDown(GLFW_MOUSE_BUTTON_LEFT);
	if(!m_bMouseAlreadyDown && down)
	{
		m_v2f_MouseInitial = m_v2f_MousePos;
		m_bMouseAlreadyDown = true;
	}

	if(m_bMouseAlreadyDown && !down)
		m_bMouseAlreadyDown = false;
}

void CInputManager::GetMousePosition(int32_t& x, int32_t& y)
{
	glfwGetMousePos(&x, &y);
}

void CInputManager::GetLastMousePosition(int32_t& x, int32_t& y)
{
	x = static_cast<int32_t>(m_v2f_LastMousePos.x);
	y = static_cast<int32_t>(m_v2f_LastMousePos.y);
}

void CInputManager::GetMouseDownPos(int32_t& x, int32_t& y)
{
	x = static_cast<int32_t>(m_v2f_MouseInitial.x);
	y = static_cast<int32_t>(m_v2f_MouseInitial.y);
}

bool CInputManager::IsMouseButtonDown(const int32_t button)
{
	int32_t isButton = glfwGetMouseButton(button);
	return isButton == GLFW_PRESS ? true : false;
}

void CInputManager::UpdateMouse()
{
	int32_t mouseX, mouseY;
	mouseX = mouseY = 0;
	CInputManager::GetMousePosition(mouseX, mouseY);
	m_v2f_LastMousePos.x = static_cast<float>(mouseX);
	m_v2f_LastMousePos.y = static_cast<float>(mouseY);
}
